package com.ark.cloud.dataservices.modules.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ark.cloud.dataservices.modules.sys.dao.DataOperationMapper;
import com.ark.cloud.dataservices.modules.sys.service.IDataOperationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DataOperationService implements IDataOperationService {

    final DataOperationMapper dataOperationMapper;

    public DataOperationService(DataOperationMapper dataOperationMapper) {
        this.dataOperationMapper = dataOperationMapper;
    }


    @Override
    public JSONArray excuteDQL(String dql) {
        List<Map<String, Object>> maps = dataOperationMapper.selectPublicItem(dql);
        return JSONObject.parseArray(JSON.toJSONString(maps));
    }

    @Override
    public boolean excuteDML(String dml) {
        String dmlSql = dml.toLowerCase();
        if(dmlSql.startsWith("insert")) {
            return dataOperationMapper.insertPublicItem(dml) > 0;
        } else if (dmlSql.startsWith("update")) {
            return dataOperationMapper.updatePublicItem(dml) > 0;
        } else if (dmlSql.startsWith("delete")) {
            return dataOperationMapper.deletePublicItem(dml) > 0;
        } else {
            return false;
        }
    }
}
